package com.tuocent.dagv2.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tuocent.dagv2.common.AjaxResult;
import com.tuocent.dagv2.constant.PublicConst;
import com.tuocent.dagv2.entity.ArchivesFiles;
import com.tuocent.dagv2.entity.FieldConfig;
import com.tuocent.dagv2.mapper.ArchivesFilesMapper;
import com.tuocent.dagv2.service.IArchivesFilesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tuocent.dagv2.service.IFieldConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.function.Consumer;

import static com.tuocent.dagv2.common.utils.PublicUtil.sortMap;

/**
 * <p>
 * 档案文件目录 服务实现类
 * </p>
 *
 * @author Vincent
 * @since 2025-09-12
 */
@Service
public class ArchivesFilesServiceImpl extends ServiceImpl<ArchivesFilesMapper, ArchivesFiles> implements IArchivesFilesService {
    @Autowired
    private IFieldConfigService fieldConfigService;

    @Override
    public AjaxResult getArchivesFile(Map<String, Object> params) {
        int page = PublicConst.DEF_PAGE;
        int limit = PublicConst.DEF_LIMIT;
        String classifyId = params.getOrDefault("classifyId", "").toString();
        String value = "";
        QueryWrapper<ArchivesFiles> query = new QueryWrapper<ArchivesFiles>()
                .eq("is_del", "0");
        for (Object obj : params.keySet()) {
            String key = obj.toString();
            String keyValue = params.get(key).toString();
            if (keyValue.trim().isEmpty() || key.equals("classifyId")) {
                continue;
            }
            switch (key) {
                case "page":
                    page = Integer.parseInt(keyValue);
                    break;
                case "limit":
                    limit = Integer.parseInt(keyValue);
                    break;
                case "value":
                    value = keyValue;
                    break;
                default:
                    query.eq(key, keyValue);
                    break;
            }
        }

        //获取字段配置
        List<FieldConfig> fieldsList = fieldConfigService.getFieldConfig(classifyId, "2");
        Map<String, String> fastsearch = new HashMap<>();
        Map<String, Integer> sort = new HashMap<>();
        Collection<String> mixSort = new ArrayList<>();
        //获取排序、检索字段
        for (FieldConfig filed : fieldsList) {
            if ("1".equals(filed.getIsSort())) {
                int dix = 999;
                if (filed.getSortIdx() != null) {
                    dix = filed.getSortIdx();
                }
                sort.put(filed.getFieldName(), dix);
            }
            if ("1".equals(filed.getIsmixSort())) {
                mixSort.add(filed.getFieldName());
            }
            if ("1".equals(filed.getIsFastsearch())) {
                fastsearch.put(filed.getFieldName(), filed.getIsVaguesearch());
            }
        }

        //检索,如果没有可快速查询字段，侧返回空值
        if (!value.isEmpty()) {
            String finalValue = value;
            Consumer<QueryWrapper<ArchivesFiles>> wrapper = new Consumer<QueryWrapper<ArchivesFiles>>() {
                @Override
                public void accept(QueryWrapper<ArchivesFiles> archivesFilesQueryWrapper) {
                    for (String key : fastsearch.keySet()) {
                        if ("1".equals(fastsearch.get(key))) {
                            archivesFilesQueryWrapper.or().like(key, finalValue);
                        } else {
                            archivesFilesQueryWrapper.or().eq(key, finalValue);
                        }
                    }
                    archivesFilesQueryWrapper.or().eq("batchcode", finalValue);
                }
            };
            query.and(wrapper);
        }
        //排序
        if (!sort.isEmpty()) {
            Map<String, Integer> sortField = sortMap(sort);
            for (String key : sortField.keySet()) {
                if (mixSort.contains(key)) {
                    query.orderByAsc("length(" + key + ")," + key);
                } else {
                    query.orderByAsc(key);
                }
            }
        }
        Page<ArchivesFiles> archiveFiles = page(new Page<>(page, limit), query);
        return AjaxResult.success(archiveFiles.getRecords(), archiveFiles.getTotal());
    }

    public AjaxResult getBatchArchivesFile(String pid, String tid, String batchCode) {
        List<ArchivesFiles> data = list(new QueryWrapper<ArchivesFiles>()
                .eq("pid", pid)
                .eq("tid", tid)
                .eq("batch_code", batchCode)
                .eq("is_del", "0"));

        return AjaxResult.success(data, Long.valueOf(data.size()));
    }

    public AjaxResult getOneArchivesFile(String pid, String tid, String id) {
        ArchivesFiles data = getOne(new QueryWrapper<ArchivesFiles>()
                .eq("pid", pid)
                .eq("tid", tid)
                .eq("id", id)
                .eq("is_del", "0"));
        return AjaxResult.success(data);
    }
}
